package com.jy.appmarket.screenlock.activity;

import com.jy.appmarket.R;
import com.jy.appmarket.screenlock.LockScreenManager;
import com.jy.appmarket.screenlock.LockScreenManager.ILockScreenListener;
import com.jy.appmarket.screenlock.LockSoundManager;
import com.jy.appmarket.screenlock.prefs.LockPrefs;
import com.umeng.analytics.MobclickAgent;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

/**
 * 类说明： 锁屏界面
 * 
 * @author xinhui.cheng
 * @date 2015-2-9
 * @version 1.0
 */
public class TBLockScreenActivity extends Activity {

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!LockScreenManager.getInstance(this).isLocked()) {
			finish();
		}

		if (Build.VERSION.SDK_INT >= 19) {
			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		LockScreenManager.getInstance(this).setOnLockScreenListener(new ILockScreenListener() {

			@Override
			public void onLock() {
			}

			@Override
			public void onUnLock() {
				// if (getFileDownloadService() != null && info !=null) {
				// FileDownloadModel fdm = new FileDownloadModel();
				// fdm.setAppName(info.getAppName());
				// fdm.setDlUrl(info.getDownloadUrl());
				// //fdm.setIconUrl(info.getIconUrl());
				// fdm.setPkgName(info.getPkgName());
				// fdm.setAutoInstall(1);
				// fdm.setHidden(0);
				// getFileDownloadService().startDownload(fdm);
				// //跳到下载管理页面
				//// Intent intent = new Intent(TBLockScreenActivity.this,
				//// DownloadMgrActivity.class);
				//// intent.putExtra(UnDownloadedFragment.COME_PAGE,UnDownloadedFragment.FROM_LOCK_RECOMMEND);
				//// startActivity(intent);
				// }

				finish();
				overridePendingTransition(R.anim.alpha_in_anim, R.anim.alpha_out_anim);
				boolean lockScreenVoice = LockPrefs.isLockSoundOpen(TBLockScreenActivity.this);
				if (lockScreenVoice) {
					LockSoundManager.play(TBLockScreenActivity.this, LockSoundManager.SOUND_ID_UNLOCK);
				}
			}

			@Override
			public void onInitDefaultImage() {
				// overridePendingTransition(R.anim.umeng_fb_slide_in_from_right,
				// R.anim.umeng_fb_slide_out_from_left);
				finish();
			}
		});
	}

	@TargetApi(Build.VERSION_CODES.KITKAT)
	@Override
	protected void onResume() {
		super.onResume();
		int systemUI = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
		if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
			systemUI |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
		}
		// systemUI |= View.SYSTEM_UI_FLAG_FULLSCREEN;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		if (Build.VERSION.SDK_INT >= 19) {
			systemUI |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
		}
		getWindow().getDecorView().setSystemUiVisibility(systemUI);
		MobclickAgent.onResume(this); // 统计时长
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        return keyCode == KeyEvent.KEYCODE_BACK;
    }

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
